package com.ikkong.sn.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ikkong.common.vo.ShiroUser;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.sn.model.SnSupplier;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.sn.model.SnOrderIn;
import com.ikkong.sn.service.SnOrderInService;
import com.ikkong.sn.service.impl.SnOrderInServiceImpl;
import org.beetl.sql.core.kit.StringKit;

/**
 * Generated by Blade.
 * 2019-04-25 21:36:08
 */
public class SnOrderInController extends UrlPermissController {
	private static String CODE = "snOrderIn";
	private static String PERFIX = "sn_order_in";
	private static String LIST_SOURCE = "SnOrderIn.list";
	private static String BASE_PATH = "/platform/snOrderIn/";
	
	SnOrderInService service = new SnOrderInServiceImpl();
	
	public void index() {
        String s = ShiroKit.getUser().getRoleList().get(0);
        ShiroUser user = ShiroKit.getUser();
        setAttr("deptid",ShiroKit.getUser().getDeptId());
        setAttr("role",s);
		setAttr("code", CODE);
		render(BASE_PATH + "snOrderIn.html");
	}

	@ClearShiro
	public void pass(){
        String status = getPara("status");
        if(StringKit.isBlank(status)){
            renderJson(error("没有状态 值"));
            return;
        }
        String orderId = getPara("orderId");
        if(StringKit.isBlank(orderId)){
            renderJson(error("未传入单号"));
            return;
        }

        Map para = new HashMap();
        para.put("status",status);
        para.put("orderId",orderId);
        String opstep1 = getPara("opstep1");
        String opstep2 = getPara("opstep2");
        String opstep3 = getPara("opstep3");
        String opstep4 = getPara("opstep4");
        if(StringKit.isNotBlank(opstep1)){
            para.put("opstep1",opstep1);
        }
        if(StringKit.isNotBlank(opstep2)){
            para.put("opstep2",opstep2);
        }
        if(StringKit.isNotBlank(opstep3)){
            para.put("opstep3",opstep3);
        }
        if(StringKit.isNotBlank(opstep4)){
            para.put("opstep4",opstep4);
        }
        int update = Blade.dao().update("SnOrderIn.updateStatus", para);
        if(update>0){
            renderJson(success("操作成功"));
        }else {
            renderJson(error("操作失败"));
        }
    }
	public void add() {

        List<SnSupplier> suppliers = Blade.create(SnSupplier.class).findAll();
		List<Map> company = Blade.dao().select("SnOrderIn.company",Map.class,null);
        SnOrderIn snOrderIn = new SnOrderIn();
		snOrderIn.setAddtime(new Date());
		snOrderIn.setStatus(-1);
		snOrderIn.setIs_store(0);
		snOrderIn.setD_id((Integer)ShiroKit.getUser().getDeptId());
		snOrderIn.setU_id((Integer)ShiroKit.getUser().getId());
        int id = service.saveRtId(snOrderIn);
        setAttr("suppliers",suppliers);
        setAttr("company",company);
        setAttr("orderId",id);
        setAttr("code", CODE);
		render(BASE_PATH + "snOrderIn_add.html");
	}

	@ClearShiro
	public void edit() {
        String id = getPara(0);
        List<SnSupplier> suppliers = Blade.create(SnSupplier.class).findAll();
        List<Map> company = Blade.dao().select("SnOrderIn.company",Map.class,null);
        setAttr("suppliers",suppliers);
        setAttr("orderId",id);
		Map snOrderIn = Blade.dao().selectSingle("SnOrderIn.findById",Record.create().set("id",id),Map.class);
		setAttr("order",snOrderIn);
        setAttr("company",company);
		setAttr("model", JsonKit.toJson(snOrderIn));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "snOrderIn_edit.html");
	}

	public void view() {
		String id = getPara(0);
		setAttr("orderId",id);
		Map snOrderIn = Blade.dao().selectSingle("SnOrderIn.findById",Record.create().set("id",id),Map.class);
		setAttr("order",snOrderIn);
		setAttr("model", JsonKit.toJson(snOrderIn));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "snOrderIn_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		SnOrderIn snOrderIn = mapping(PERFIX, SnOrderIn.class);
		boolean temp = service.save(snOrderIn);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		SnOrderIn snOrderIn = mapping(PERFIX, SnOrderIn.class);
		boolean temp = service.update(snOrderIn);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
